// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var CardSpriteType = require('MJCommon').CardSpriteType;
cc.Class({
    extends: cc.Component,
    mOut: null, //出牌的数据
    mHand: null, //手牌的数据
    mCPG: null, //吃碰杠的数据
    mShow: null, //显牌时的数据
    mHit: null, //提示时的数据
    // mGet: null, //获取时
    properties: {
        parent: cc.Node,
        hand1: cc.Node,
        hand2: cc.Node,
        cpg: cc.Node,
        cpg1: cc.Node,
        cpg2: cc.Node,
        cpg3: cc.Node,
        show1: cc.Node,
        show2: cc.Node,
        out1: cc.Node,
        out2: cc.Node,
        out3: cc.Node,
        hint: cc.Node, 
        outColumn: 14
    },

    copy: function (data) {

    },

    getMap(type) {
        switch (type) {
            case CardSpriteType.cstHandCard:
                return this.mHit;
            case CardSpriteType.cstHandBack:
            case CardSpriteType.cstHandDown:
                return this.mHand;
            case CardSpriteType.cstHandShow:
                return this.mShow;
            case CardSpriteType.cstCPG:
            case CardSpriteType.cstCPGBack:
                return this.mCPG;
            case CardSpriteType.cstOutCard:
                return this.mOut;
            case CardSpriteType.cstHandGet:
                return this.mGet;
        }
    },

    onLoad() {
        this.mOut = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: 0, //间隔
            faceRotate: 0, //旋转
            othNums: 0, //第二间隔的数据
            startPos: cc.p(0, 0), //开始时的坐标
            startPos2: cc.p(0, 0) //第二间隔
        };
        this.mHand = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: 0, //间隔
            faceRotate: 0, //旋转
            startPos: cc.p(0, 0), //开始时的坐标 
        };
        this.mCPG = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: cc.p(0, 0), //间隔
            fix2: cc.p(0, 0), //上下间隔
            fix3: cc.p(0, 0), //相互之间的间隔
            faceRotate: 0, //旋转 
            startPos: cc.p(0, 0), //开始时的坐标 
        };
        this.mShow = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: 0, //间隔
            faceRotate: 0, //旋转 
            startPos: cc.p(0, 0), //开始时的坐标
            startPos2: cc.p(0, 0) //第二间隔
        };
        this.mHit = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: 0, //间隔
            faceRotate: 0, //旋转 
            startPos: cc.p(0, 0), //开始时的坐标 
        };

        this.mGet = {
            bgFileName: '',
            backFileName: '',
            scale: 1,
            facePos: cc.p(0, 0), //坐标
            faceScale: cc.p(0, 0), //缩放
            fix: 0, //间隔
            faceRotate: 0, //旋转 
            startPos: cc.p(0, 0), //开始时的坐标 
        };

        var basePos = this.parent.getPosition();


        this.mHit.scale = this.hint.getScale();
        if (this.hint.getComponent(cc.Sprite)._spriteFrame) {
            this.mHit.bgFileName = this.hint.getComponent(cc.Sprite)._spriteFrame.name;
        }
        if (this.hint.getChildByName('face').getComponent(cc.Sprite)) {
            this.mHit.facePos = this.hint.getChildByName('face').getPosition();
            this.mHit.faceScale.x = this.hint.getChildByName('face').getScaleX();
            this.mHit.faceScale.y = this.hint.getChildByName('face').getScaleY();
            this.mHit.faceRotate = this.hint.getChildByName('face').getRotation();
        }

        this.mHit.startPos = cc.pAdd(this.hint.getPosition(), basePos);

        // this.mGet.scale = this.getcard.getScale();
        // if (this.getcard.getComponent(cc.Sprite)._spriteFrame) {
        //     this.mGet.bgFileName = this.getcard.getComponent(cc.Sprite)._spriteFrame.name;
        // }
        // // if (this.hint.getChildByName('face').getComponent(cc.Sprite)) {
        // //     this.mGet.facePos = this.getcard.getChildByName('face').getPosition();
        // //     this.mGet.faceScale.x = this.getcard.getChildByName('face').getScaleX();
        // //     this.mGet.faceScale.y = this.getcard.getChildByName('face').getScaleY();
        // //     this.mGet.faceRotate = this.getcard.getChildByName('face').getRotation();
        // // }

        // this.mGet.startPos = cc.pAdd(this.getcard.getPosition(), basePos);


        this.mCPG.scale = this.cpg.getScale();
        if (this.cpg.getComponent(cc.Sprite)._spriteFrame) {
            this.mCPG.bgFileName = this.cpg.getComponent(cc.Sprite)._spriteFrame.name;
        }

        if (this.cpg1.getComponent(cc.Sprite)._spriteFrame) {
            this.mCPG.backFileName = this.cpg1.getComponent(cc.Sprite)._spriteFrame.name;
        }

        if (this.cpg.getChildByName('face').getComponent(cc.Sprite)) {
            this.mCPG.facePos = this.cpg.getChildByName('face').getPosition();
            this.mCPG.faceScale.x = this.cpg.getChildByName('face').getScaleX();
            this.mCPG.faceScale.y = this.cpg.getChildByName('face').getScaleY();
            this.mCPG.faceRotate = this.cpg.getChildByName('face').getRotation();
        }

        if (this.cpg1.getComponent(cc.Sprite)._spriteFrame) {
            this.mCPG.backFileName = this.cpg1.getComponent(cc.Sprite)._spriteFrame.name;
        }


        if (this.cpg2.getComponent(cc.Sprite)._spriteFrame) {
            this.mCPG.backFileName = this.cpg2.getComponent(cc.Sprite)._spriteFrame.name;
        }


        this.mHand.scale = this.hand1.getScale();
        if (this.hand1.getComponent(cc.Sprite)._spriteFrame) {
            this.mHand.bgFileName = this.hand1.getComponent(cc.Sprite)._spriteFrame.name;
        }

        if (this.hand2.getComponent(cc.Sprite)._spriteFrame) {
            this.mHand.backFileName = this.hand2.getComponent(cc.Sprite)._spriteFrame.name;
        }


        if (this.hand1.getChildByName('face').getComponent(cc.Sprite)) {
            this.mHand.facePos = this.hand1.getChildByName('face').getPosition();
            this.mHand.faceScale.x = this.hand1.getChildByName('face').getScaleX();
            this.mHand.faceScale.y = this.hand1.getChildByName('face').getScaleY();
            this.mHand.faceRotate = this.hand1.getChildByName('face').getRotation();
        }



        this.mShow.scale = this.show1.getScale();
        if (this.show1.getComponent(cc.Sprite)._spriteFrame) {
            this.mShow.bgFileName = this.show1.getComponent(cc.Sprite)._spriteFrame.name;
        }

        if (this.show2.getComponent(cc.Sprite)._spriteFrame) {
            this.mShow.backFileName = this.show2.getComponent(cc.Sprite)._spriteFrame.name;
        }


        if (this.show1.getChildByName('face').getComponent(cc.Sprite)) {
            this.mShow.facePos = this.show1.getChildByName('face').getPosition();
            this.mShow.faceScale.x = this.show1.getChildByName('face').getScaleX();
            this.mShow.faceScale.y = this.show1.getChildByName('face').getScaleY();
            this.mShow.faceRotate = this.show1.getChildByName('face').getRotation();
        }


        this.mOut.scale = this.out1.getScale();
        if (this.out1.getComponent(cc.Sprite)._spriteFrame) {
            this.mOut.bgFileName = this.out1.getComponent(cc.Sprite)._spriteFrame.name;
            this.mOut.backFileName = this.out1.getComponent(cc.Sprite)._spriteFrame.name;
        }

        if (this.out1.getChildByName('face').getComponent(cc.Sprite)) {
            this.mOut.facePos = this.out1.getChildByName('face').getPosition();
            this.mOut.faceScale.x = this.out1.getChildByName('face').getScaleX();
            this.mOut.faceScale.y = this.out1.getChildByName('face').getScaleY();
            this.mOut.faceRotate = this.out1.getChildByName('face').getRotation();
        }



        

        this.mCPG.startPos = cc.pAdd(this.cpg.getPosition(), basePos);
        this.mHand.startPos = cc.pAdd(this.cpg.getPosition(), basePos);
        this.mShow.startPos = cc.pAdd(this.cpg.getPosition(), basePos);
        this.mOut.startPos = cc.pAdd(this.out1.getPosition(), basePos);

        this.mCPG.fix = cc.pSub(this.cpg1.getPosition(), this.cpg.getPosition());
        this.mCPG.fix2 = cc.pSub(this.cpg2.getPosition(), this.cpg1.getPosition());
        this.mCPG.fix3 = cc.pSub(this.hand1.getPosition(), this.cpg3.getPosition());
        this.mCPG.fix3 = cc.pSub(this.mCPG.fix3, this.mCPG.fix);

        this.mOut.fix = cc.pSub(this.out2.getPosition(), this.out1.getPosition());
        this.mOut.fix2 = cc.pSub(this.out3.getPosition(), this.out1.getPosition());

        this.mHand.fix = cc.pSub(this.hand2.getPosition(), this.hand1.getPosition());
        this.mShow.fix = cc.pSub(this.show2.getPosition(), this.show1.getPosition());

        if (this.mCPG.fix.x == 0) {
            this.mCPG.fix3.x = 0;
        } else if (this.mCPG.fix.y == 0) {
            this.mCPG.fix3.y = 0;
            //横向排列 主要判断y的不同
            this.mHand.startPos.y = this.hand1.getPosition().y + basePos.y;
            this.mShow.startPos.y = this.show1.getPosition().y + basePos.y;
        }
        // for(var i = 0;i<this.chi)
        // this.retain();
        // this.parent.destroy();

        var childrens = this.parent.children;
        for (var i = 0; i < childrens.length; i++) {
            childrens[i].destroy();
        }

        //         children
        // :
        // Array(23)
        // childrenCount
        // :
        // 23


    },

    onDestroy() {
        cc.log('onDestroy');
    },

    start() {


        // mBgFileName: null, //正面图片
        // mBackFileName: null, //背面的图片
        // mScale: 0, //缩放大小比例
        // mFacePos: 0, //牌面缩小的比例
        // mFaceScale: 0, //牌面缩小的比例
        // mFix: 0, //间隔
        // mFaceRotate: 0, //旋转
        // mOthNums: 0, //使用第二间隔时的的index
        // mStartPos: 0, //开始坐标
        // mStartPos2: 0, //出牌时第二个间隔
    },

    // update (dt) {},
});